package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.ZjGroutingReportEdit;
import com.ruoyi.zhgd.mapper.ZjGroutingReportEditMapper;
import com.ruoyi.zhgd.mapper.ZjTensionReportEditMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.GroutingRecordBo;
import com.ruoyi.zhgd.domain.vo.GroutingRecordVo;
import com.ruoyi.zhgd.domain.GroutingRecord;
import com.ruoyi.zhgd.mapper.GroutingRecordMapper;
import com.ruoyi.zhgd.service.IGroutingRecordService;

import java.util.*;

/**
 * 压浆记录Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class GroutingRecordServiceImpl implements IGroutingRecordService {

    private final GroutingRecordMapper baseMapper;

    private final ZjTensionReportEditMapper zjTensionReportEditMapper;

    private final ZjGroutingReportEditMapper zjGroutingReportEditMapper;

    /**
     * 查询压浆记录
     */
    @Override
    public GroutingRecordVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询压浆记录列表
     */
    @Override
    public TableDataInfo<GroutingRecordVo> queryPageList(GroutingRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GroutingRecord> lqw = buildQueryWrapper(bo);
        Page<GroutingRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压浆记录列表
     */
    @Override
    public List<GroutingRecordVo> queryList(GroutingRecordBo bo) {
        LambdaQueryWrapper<GroutingRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GroutingRecord> buildQueryWrapper(GroutingRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GroutingRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), GroutingRecord::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTensioningBeamType()), GroutingRecord::getTensioningBeamType, bo.getTensioningBeamType());
        lqw.eq(StringUtils.isNotBlank(bo.getBeamNumber()), GroutingRecord::getBeamNumber, bo.getBeamNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPileNumber()), GroutingRecord::getPileNumber, bo.getPileNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getGroutAgent()), GroutingRecord::getGroutAgent, bo.getGroutAgent());
        lqw.eq(StringUtils.isNotBlank(bo.getCement()), GroutingRecord::getCement, bo.getCement());
        lqw.eq(StringUtils.isNotBlank(bo.getDutyPersonnel()), GroutingRecord::getDutyPersonnel, bo.getDutyPersonnel());
        lqw.eq(bo.getBoreholeCount() != null, GroutingRecord::getBoreholeCount, bo.getBoreholeCount());
        lqw.eq(StringUtils.isNotBlank(bo.getGroutingDirection()), GroutingRecord::getGroutingDirection, bo.getGroutingDirection());
        lqw.eq(bo.getStepCount() != null, GroutingRecord::getStepCount, bo.getStepCount());
        lqw.eq(StringUtils.isNotBlank(bo.getStepParameters()), GroutingRecord::getStepParameters, bo.getStepParameters());
        lqw.eq(bo.getInitialFlowVelocity() != null, GroutingRecord::getInitialFlowVelocity, bo.getInitialFlowVelocity());
        lqw.eq(bo.getFlowabilityS() != null, GroutingRecord::getFlowabilityS, bo.getFlowabilityS());
        return lqw;
    }

    /**
     * 新增压浆记录
     */
    @Override
    public Boolean insertByBo(GroutingRecordBo bo) {
        GroutingRecord add = BeanUtil.toBean(bo, GroutingRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改压浆记录
     */
    @Override
    public Boolean updateByBo(GroutingRecordBo bo) {
        GroutingRecord update = BeanUtil.toBean(bo, GroutingRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GroutingRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压浆记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils recordList(Page page, Map<String, Object> params) {
        List<Map> recordList = baseMapper.recordList(page, params);
        return new PageUtils(page.setRecords(recordList));
    }

    /**
     * 压浆报表基础信息
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> listMap(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("id"))) {
            throw new ServiceException("主键ID不能为空");
        }
        // 根据部门信息获取项目信息 合同信息
        Map<String, Object> projectInfo = new HashMap<>();
        GroutingRecordVo project = baseMapper.getProjectById(params);
        if (ObjectUtils.isNotEmpty(project)) {
            projectInfo.put("beamNumber", project.getPileNumber() + project.getBeamNumber());
            projectInfo.put("projectId", project.getProjectId());
            projectInfo.put("projectName", project.getProjectName());
            projectInfo.put("contractId", project.getContractId());
            projectInfo.put("contractNum", project.getContractNum());
            projectInfo.put("sgGroup", project.getSgGroup());
            projectInfo.put("jlGroup", project.getJlGroup());
        }
        // 根据id获取报表编辑信息含图片
        QueryWrapper<ZjGroutingReportEdit> wrapper = new QueryWrapper<>();
        wrapper.eq("business_id", params.get("id"));
        ZjGroutingReportEdit edit = zjGroutingReportEditMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(edit)) {
            projectInfo.put("snpzbh", edit.getSnpzbh());
            projectInfo.put("wjj", edit.getWjj());
            projectInfo.put("snjcd", edit.getSnjcd());
            projectInfo.put("msl", edit.getMsl());
            projectInfo.put("hjTemp", edit.getHjTemp());
            projectInfo.put("jyTemp", edit.getJyTemp());
            projectInfo.put("tgCondition", edit.getTgCondition());
            projectInfo.put("mjCondition", edit.getMjCondition());
            projectInfo.put("fileUrl", edit.getFileUrl());
            projectInfo.put("fileId", edit.getFileId());
            projectInfo.put("sgDate", edit.getSgDate());
            projectInfo.put("id", edit.getId());
        } else {
            edit = new ZjGroutingReportEdit();
            projectInfo.put("snpzbh", edit.getSnpzbh());
            projectInfo.put("wjj", edit.getWjj());
            projectInfo.put("snjcd", edit.getSnjcd());
            projectInfo.put("msl", edit.getMsl());
            projectInfo.put("hjTemp", edit.getHjTemp());
            projectInfo.put("jyTemp", edit.getJyTemp());
            projectInfo.put("tgCondition", edit.getTgCondition());
            projectInfo.put("mjCondition", edit.getMjCondition());
            projectInfo.put("fileUrl", edit.getFileUrl());
            projectInfo.put("fileId", edit.getFileId());
            projectInfo.put("sgDate", edit.getSgDate());
            projectInfo.put("id", edit.getId());
        }
        // 返回值规范
        Map<String, Object> result = new HashMap<>();
//        Map<String, Object> mapResult = new HashMap();
//        for (Map<String, Object> map : list) {
//            mapResult.put(map.get("fieldKey").toString(), map.get("fieldVal"));
//        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        mapList.add(projectInfo);
        result.put("data", mapList);
        return result;
    }
}
